package me.zixing.java.javacomplier;

import javax.tools.JavaCompiler;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import java.io.File;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * Created by bingh on 2017/11/13.
 */
public class MainDemo {
    public static void main(String[] args) throws Exception{
        JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
        StandardJavaFileManager fileManager = javaCompiler.getStandardFileManager(null,null,null);
        Iterable fileObjects = fileManager.getJavaFileObjects("D://Test.java");

        String classPath = new File("target\\classes").getAbsolutePath();
        //System.out.println(classPath);
        //设置编译输出目录
        Iterable<String> options = Arrays.asList("-d",classPath);
        javaCompiler.getTask(null,fileManager,null,options,null,fileObjects)
        .call();
        Class<?> clazz = Class.forName("me.zixing.java.javacomplier.Test");
        Object test = clazz.newInstance();
        Method showMethod = clazz.getMethod("show");
        showMethod.invoke(test);
    }
}
